﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace BeatingMole
{
    class CBusMap
    {
        #region "Variable"
        private ContentManager _content;
        private SpriteBatch _spriteBatch;
        private GameTime _gametime;

        Random _rnd;
        private List<CBusEnemy> _listEnemy;                 // Danh sách các Enemy trong màn chơi hiện tại
        private CBusListEnemy _busListEnemy;                // Danh sách các Enemy trong màn chơi hiện tại
        public CInfoMap _map;                               // CInfoMap
        private CBusButton BusButton;                       // CInfoButton
        private CBusOrtherSettings BusOrtherSettings;       // CBusOtherSetting
        private CBusWeapons BusWeapons;                     // CBusWeapons
        private CBusPlayer BusPlayer;                       // CBusPlayer
        private CBusMouseMove BusMouseMove;                 // CBusMouseMove
        private CBusGameResult BusGameResult;               // Tổng kết màn chơi

        bool IsPlay = true;
        #endregion

        #region "Properties"

        #endregion

        #region "Constructor"

        /// <summary>
        /// (Nguyễn Kim Nhất Quý)
        /// Khởi tạo 3 tham số hệ thống với Enemy đầu tiên trong ListEnemey
        /// </summary>
        /// <param name="content"></param>
        /// <param name="spriteBatch"></param>
        /// <param name="gameTime"></param>
        public CBusMap(ContentManager content, SpriteBatch spriteBatch, GameTime gameTime)
        {
            _rnd = new Random();
            _content = content;
            _spriteBatch = spriteBatch;
            _gametime = gameTime;

            BusButton = new CBusButton(content);
            BusOrtherSettings = new CBusOrtherSettings(content);
            BusWeapons = new CBusWeapons(content);
            BusPlayer = new CBusPlayer(content);
            BusMouseMove = new CBusMouseMove(content);
            BusGameResult = new CBusGameResult(content, spriteBatch, gameTime);

            _busListEnemy = new CBusListEnemy(_rnd, content, spriteBatch, gameTime);
            _listEnemy = _busListEnemy.ListEnemy;
            switch (CGlobalVariable.level)
            {
                case 1: _map = new CInfoMap1(content, spriteBatch, gameTime); break;
                case 2: _map = new CInfoMap2(content, spriteBatch, gameTime); break;
                case 3: _map = new CInfoMap3(content, spriteBatch, gameTime); break;
                case 4: _map = new CInfoMap4(content, spriteBatch, gameTime); break;
            }
            if (CGlobalVariable.IsStart == true)
            {
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Play(_map.SongPlayGame);
            }
        }
        #endregion

        #region "Methods"
        /// <summary>
        /// Phương thức cập nhật của Map
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            _busListEnemy.UpdateBusListEnemy(gameTime);            
        }

        /// <summary>
        /// Phương thức Draw của Map
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_map.Background, Vector2.Zero, Color.White);
            _busListEnemy.DrawBusListEnemy(spriteBatch);            
        }

        #endregion

        #region "Other methods"
        public void UpdateMap(GameTime gameTime)
        {            
            if (_map.IsEnable == true)
            {
                if (_map.TimeMap <= 0 && (CGlobalVariable.InfoPlayer.Score < _map.RequiredScore))
                {
                    CGlobalVariable.IsGameOver = true;
                    CGlobalVariable.IsWin = false;
                }
                else if (_map.TimeMap <= 0 && (CGlobalVariable.InfoPlayer.Score >= _map.RequiredScore))
                {
                    CGlobalVariable.IsGameOver = false;
                    CGlobalVariable.IsWin = true;                    
                }
                _map.TimeUpdate += (float)gameTime.ElapsedGameTime.Milliseconds;
                if (_map.TimeUpdate > 1000)
                {
                    _map.TimeUpdate = 0;
                    _map.TimeMap -= 1; // => Thời gian màn chơi giãm 1 giây . Khi hết giờ thì thua.
                }
            }
        }

        public void UpdateResumeGame(GameTime gameTime)
        {
            CGlobalVariable.mouseStateCurrent = Mouse.GetState();
            if (CGlobalVariable.mouseStateCurrent.LeftButton == ButtonState.Pressed && CGlobalVariable.mouseStatePrevious.LeftButton == ButtonState.Released)
            {
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(new Rectangle(0, 0, _map.Pause.Width, _map.Pause.Height)))
                {
                    CGlobalVariable.IsPauseGame = false;
                }
            }
            CGlobalVariable.mouseStatePrevious = CGlobalVariable.mouseStateCurrent;
        }

        public void DrawTime(SpriteBatch spriteBatch)
        {
            if (CGlobalVariable.IsWin == false)
            {
                spriteBatch.Draw(_map.Clock, new Vector2(256, 0), Color.White);
                spriteBatch.Draw(_map.TimeBar, new Vector2(320, 16), new Rectangle(0, 0, _map.TimeMap, 32), Color.White);
            }            
        }

        public void DrawMap(SpriteBatch spriteBatch)
        {
            if (_map.IsEnable == true)
            {
                if (_map.IsVisibility == true)
                {
                    if (CGlobalVariable.IsWin == false)
                    {
                        spriteBatch.Draw(_map.Bar, Vector2.Zero, Color.Green * 0.75f); // => Opacity 50%
                    }                    
                }
            }
        }

        public void DrawPauseGame(SpriteBatch spriteBatch)
        {
            if (CGlobalVariable.IsPauseGame == true)
            {
                spriteBatch.Draw(_map.Pause, Vector2.Zero, Color.Black * 0.75f);
                spriteBatch.Draw(_map.TextPauseGame, new Vector2(200, 125), Color.White);
            }
        }

        public void PlayOverSong()
        {
            if (IsPlay == true)
            {
                _map.SongGameOver.Play();
            }
        }

        public void DrawGameOver(SpriteBatch spriteBatch)
        {
            if (CGlobalVariable.IsGameOver == true)
            {
                MediaPlayer.Stop();
                PlayOverSong();
                IsPlay = false;
                spriteBatch.Draw(_map.Pause, Vector2.Zero, Color.Black * 0.75f);
                spriteBatch.Draw(_map.TextGameOver, new Vector2(200, 125), Color.White);
            }            
        }
        #endregion

        #region Button
        public void UpdateButton(GameTime gameTime)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusButton.UpdateButton(gameTime);
            }
        }

        public void DrawButton(SpriteBatch spriteBatch)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusButton.DrawButton(spriteBatch);
            }
        }        
        #endregion

        #region OrtherButtonsSetting
        public void DrawButtonSettings(SpriteBatch spriteBatch)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusOrtherSettings.DrawUpdateButtonOrthers(spriteBatch);
            }
        }

        public void UpdateButtonSettings(GameTime gameTime)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusOrtherSettings.UpdateButtonOrthers(gameTime);
            }
        }
        #endregion

        #region MouseMove
        public void UpdateMouseMove(GameTime gameTime)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusMouseMove.UpdateMouse(gameTime);
            }
        }

        public void DrawMouseMove(SpriteBatch spriteBatch)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusMouseMove.DrawMouse(spriteBatch);
            }
        }
        #endregion

        #region "Weapons"
        public void UpdateWeapons(GameTime gameTime)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusWeapons.UpdateWeapons(gameTime);
            }
        }

        public void DrawWeapons(SpriteBatch spriteBatch)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusWeapons.DrawWeapons(spriteBatch);
            }
        }
        #endregion

        #region "Player"
        public void UpdatePlayer(GameTime gameTime)
        {
            if (CGlobalVariable.IsWin == false)
            {
                BusPlayer.UpdatePlayer(gameTime);            
            }
        }

        public void DrawPlayer(SpriteBatch spriteBatch)
        {
            if (CGlobalVariable.IsWin == false)
            {
                BusPlayer.DrawPlayer(spriteBatch);
            }
        }

        public void DrawNamePlayer(SpriteBatch spriteBatch)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusPlayer.DrawNamePlayer(spriteBatch);
            }
        }

        public void DrawScorePlayer(SpriteBatch spriteBatch)
        {
            if (!CGlobalVariable.IsWin)
            {
                BusPlayer.DrawScorePlayer(spriteBatch);
            }
        }
        #endregion

        #region "Win"
        /// <summary>
        ///  Cập nhật chiến thắng cho màn chơi
        /// </summary>
        /// <param name="gameTime"></param>
        public void UpdateWin(GameTime gameTime)
        {
            if (CGlobalVariable.IsWin)
            {
                BusGameResult.Update(gameTime);            
            }            
        }

        /// <summary>
        ///  Draw chiến thắng cho màn chơi
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void DrawWin(SpriteBatch spriteBatch)
        {
            if (CGlobalVariable.IsWin)
            {
                BusGameResult.Draw(spriteBatch);
            }
        }
        #endregion
    }
}
